package org.nf.vrs.userservice.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.nf.vrs.userservice.dao.UserDao;
import org.nf.vrs.userservice.dto.PageDTO;
import org.nf.vrs.userservice.dto.UserDTO;
import org.nf.vrs.userservice.entity.User;
import org.nf.vrs.userservice.inf.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author duang
 * @date 2022/3/29
 */
@Slf4j
@DubboService(interfaceClass = UserService.class, version = "1.0.0")
@Transactional(rollbackFor = RuntimeException.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao dao;

    @Override
    public PageDTO<UserDTO> listUser(UserDTO userDTO, int pageNum, int pageSize) {
        User user = BeanUtil.copyProperties(userDTO, User.class);

        List<User> list = dao.listUser(user, pageNum, pageSize);

        PageInfo<User> pageInfo = new PageInfo<>(list);

        PageDTO pageDTO = BeanUtil.copyProperties(pageInfo, PageDTO.class);

        return pageDTO;
    }

    @Override
    public UserDTO getUserById(int id) {
        User user = dao.getUserById(id);

        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);

        return userDTO;
    }

    @Override
    public List<UserDTO> queryUserByName(String userName) {
        List<UserDTO> userDTOList = new ArrayList<>();

        List<User> list = dao.queryUserByName(userName);

        for (User user : list) {
            UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
            userDTOList.add(userDTO);
        }

        return userDTOList;
    }

    @Override
    public UserDTO getUserByTel(String tel) {
        User user = dao.findUserByTel(tel);

        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);

        return userDTO;
    }

    @Override
    public void addUser(UserDTO userDTO) {
        try {
            User user = BeanUtil.copyProperties(userDTO, User.class);
            dao.addUser(user);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException("保存用户失败",e);
        }
    }

    @Override
    public void delUser(int id) {
        try {
            dao.delUser(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException("删除用户失败",e);
        }
    }

    @Override
    public void updateUser(UserDTO userDTO) {
        try {
            User user = BeanUtil.copyProperties(userDTO, User.class);
            dao.updateUser(user);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException("更新用户信息失败",e);
        }
    }

    @Override
    public void changePwd(UserDTO userDTO, String oldPwd, String newPwd) {
        if (userDTO!=null && !userDTO.getUserPassword().equals(oldPwd)) {
            throw new RuntimeException("原密码错误");
        }

        dao.changePwd(userDTO.getUid(),newPwd);
    }

    @Override
    public void changeUserStatus(int id, boolean userStatus) {
        dao.updateUserStatus(id,userStatus);
    }

    @Override
    public void breakPromise(int id) {
        dao.updateBreakCount(id);
    }
}